package cc.cix.gt.org.service.impl;

import cc.cix.gt.common.entity.ResultPage;
import cc.cix.gt.common.util.SystemUtil;
import cc.cix.gt.common.util.SortUtil;
import cc.cix.gt.org.dto.query.QueryLoginLogReq;
import cc.cix.gt.org.entity.LoginLog;
import cc.cix.gt.org.mapper.LoginLogMapper;
import cc.cix.gt.org.service.ILoginLogService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author cix
 */

@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog>
        implements ILoginLogService {

    @Override
    public ResultPage<LoginLog> listLoginLog(QueryLoginLogReq dto) {
        QueryWrapper<LoginLog> qw = new QueryWrapper<>(BeanUtil.toBean(dto, LoginLog.class));
        SortUtil.handleWrapperSort(dto, qw);
        qw.lambda().eq(StrUtil.isNotBlank(dto.getUsername()), LoginLog::getUsername,
                Optional.ofNullable(dto.getUsername()).map(String::toLowerCase));
        Page<LoginLog> page = page(new Page<>(dto.getPageNum(), dto.getPageSize()), qw);
        return SystemUtil.getPage(page);
    }

    @Override
    public void saveLoginLog(LoginLog loginLog) {
        loginLog.setLoginTime(LocalDateTime.now());
        String ip = SystemUtil.getHttpServletRequestIpAddress();
        loginLog.setIp(ip);
        loginLog.setLoginLocation(SystemUtil.getHttpServletRequestIpAddress());
        save(loginLog);
    }

    @Override
    public List<LoginLog> listUserLastSevenLoginLogs(String username) {
        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(username);

        QueryLoginLogReq dto = new QueryLoginLogReq();
        dto.setPageNum(1);
        // 近7日记录
        dto.setPageSize(7);

        return listLoginLog(dto).getRows();
    }

    @Override
    public Long getTodayVisitCount() {
        return lambdaQuery()
                .between(LoginLog::getLoginTime, DateUtil.today(), DateUtil.today())
                .count();
    }

    @Override
    public Long getTodayIp() {
        return lambdaQuery()
                .between(LoginLog::getLoginTime, DateUtil.today(), DateUtil.today())
                .groupBy(LoginLog::getIp)
                .count();
    }

    @Override
    public List<LoginLog> getLastTenVisitCount() {
        return lambdaQuery()
                .between(LoginLog::getLoginTime, DateUtil.offsetDay(
                        DateUtil.date(), 10), DateUtil.date())
                .list();
    }

    @Override
    public List<LoginLog> getLastTenUserVisitCount() {
        return lambdaQuery()
                .eq(LoginLog::getUsername, SystemUtil.getCurrentUser().getUsername())
                .between(LoginLog::getLoginTime, DateUtil.offsetDay(
                        DateUtil.date(), 10), DateUtil.date())
                .list();
    }
}
